package com.example.demo.huawei;

import java.util.*;

/**
 * 连续字母长度
 */
public class OD53 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int k = sc.nextInt();

        // 滑动窗口
        int left = 0;
        int right = 0;
        // 最大连续长度
        int maxLength = 0;
        // AAAAHHHBBCDHHHH
        int length = 0;
        Map<Character, Integer> map = new HashMap<>();
        while (right < s.length()) {
            while (right < s.length() && s.charAt(right) == s.charAt(left)) {
                length++;
                right++;
            }
            if (!map.containsKey(s.charAt(left))) {
                map.put(s.charAt(left), length);
            } else {
                int count = map.get(s.charAt(left));
                if (length > count) {
                    map.put(s.charAt(left), length);
                }
            }
            left = right;
            length = 0;
        }
        //System.out.println(map);

        List<Integer> list = new ArrayList<>(map.values());
        list.sort((o1, o2) -> o2 - o1);
        if (list.size() < k) {
            System.out.println(-1);
        } else {
            System.out.println(list.get(k - 1));
        }
    }
}
